package com.lln.controller;

import com.lln.pojo.Employee;
import com.lln.pojo.PageVo;
import com.lln.pojo.Result.Result;
import com.lln.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

//    @GetMapping
//    public Result findAll(String name){
//        if(StringUtils.hasText(name)){
//            QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
//            queryWrapper.like("name",name);
//            return Result.success(employeeService.list(queryWrapper));
//        }else{
//            List<Employee> employeeList = employeeService.list();
//            return Result.success(employeeList);
//        }
//    }
//    public Result findAll(String keyword) {
//        if (StringUtils.hasText(keyword)) {
//            QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
////            queryWrapper.like("name", keyword);
////            queryWrapper.or();
////            queryWrapper.eq("phone", keyword);
//            //链式写法
//            queryWrapper.like("name", keyword)
//                    .or()
//                    .eq("phone", keyword);
//            return Result.success(employeeService.list(queryWrapper));
//        } else {
//            List<Employee> employeeList = employeeService.list();
//            return Result.success(employeeList);
//        }
//    }

    @GetMapping
    public Result findAll(String keyword, Integer day, PageVo pageVo){
//        PageHelper.startPage(pageVo);
        return Result.success(employeeService.findSearchByKeyword(keyword,day,pageVo));
    }

    @GetMapping("/{id}")
    public Result findById(Integer id) {
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        if (employeeService.removeById(id)) {
            return Result.success("删除成功");
        } else {
            return Result.failed("删除失败");
        }
    }

    @PostMapping
    public Result add(@RequestBody Employee employee) {
        employee.setId(null);
        if (employeeService.save(employee)) {
            return Result.success("添加成功");
        } else {
            return Result.failed("添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody Employee employee) {
        if (employeeService.updateById(employee)) {
            return Result.success("修改成功");
        } else {
            return Result.failed("修改失败");
        }
    }
}
